<script>
var formStructure = [
	{type:"settings",labelWidth:100,inputWidth:200,offsetLeft:0},
	{type:"fieldset",label:"专线基本信息",name:"basic",width:350,offsetLeft:0,list:[
		{type:"input",name:"instanceId",label:"产品实例标识",validate:"^[0-9]{11}$",required:true},
		{type:"select",name:"zxType",label:"专线类别",required:true,note:{text:'目前仅支持互联网业务'}},
		{type:"select",name:"neFactory",label:"网元厂家",validate:"^[^3]+$",required:true},
		{type:"checkbox",name:"ifOnu",label:"onu 带的业务",labelWidth:80,labelAlign:"right",position:"label-right",list:[
			{type:"settings",labelWidth:80},
	        {type:"input",name:"oltName",label:"olt 名称"/*,note:{text:"如果下拉菜单搜不到请直接输入。"},*/,hidden:true},
	    ],hidden:true},
		{type:"combo",name:"aStation",label:"A 端基站",required:true},
		{type:"input",name:"bandWidth",label:"带宽(M)",value:"",validate:"^[0-9Mm]+$",required:true},
		{type:"input",name:"cName",label:"客户名称",maxLength:30,required:true},
		{type:"input",name:"cAddress",label:"客户地址",required:true},
		{type:"input",name:"cNeeds",label:"客户需求",maxLength:150,rows:3},
		{type:"input",name:"mPerson",label:"客户经理姓名",maxLength:5,required:true},
		{type:"input",name:"mPhone",label:"客户经理电话",maxLength:12,required:true},
		{type:"input",name:"mEmail",label:"客户经理邮箱",validate:"ValidEmail",required:true},
		{type:"input",name:"marks",label:"备注"},
	]},
	{type:"block",name:"btn",inputWidth:"auto",offsetLeft:30,list:[
		{type:"newcolumn"},
		{type:"button",name:"commit",value:"提交",offsetLeft:30},
		{type:"newcolumn"},
		{type:"button",name:"cancel",value:"重置",offsetLeft:30},
		/*{type:"newcolumn"},
		{type:"button",name:"test",value:"测试数据",offsetLeft:30},*/
	]},
	{type:"newcolumn",name:"newcol1",offset:10},
	{type:"fieldset",name:"ip_bak",label:"IP 备案附加信息",width:350,list:[
		{type:"input",name:"aPerson",label:"申请人",value:"{$Think.session.user.name}",readonly:true,hidden:true},
		{type:"input",name:"aEmail",label:"申请人邮箱",value:"{$Think.session.user.email}",readonly:true,hidden:true},
		{type:"input",name:"cPerson",label:"客户联系人姓名",maxLength:5,required:true},
		{type:"input",name:"cPhone",label:"客户联系人电话",maxLength:12,required:true},
		{type:"input",name:"cEmail",label:"客户联系人邮箱",validate:"ValidEmail",required:true},
		{type:"combo",name:"unitProperty",label:"单位性质",required:true},
		{type:"combo",name:"unitCategory",label:"单位分类",required:true},
		{type:"combo",name:"industryCategory",label:"行业分类",required:true},
		{type:"combo",name:"credential",label:"单位证件类型",required:true},
		{type:"input",name:"credentialnum",label:"单位证件号",required:true},
		{type:"combo",name:"province",label:"所在省",required:true,hidden:true},
		{type:"combo",name:"city",label:"所在市",required:true,hidden:true},
		{type:"combo",name:"county",label:"所在县区",required:true},
		{type:"input",name:"zipCode",label:"邮政编码",required:true},
		{type:"combo",name:"appServType",label:"应用服务类型",required:true},
		{type:"select",name:"unitAttribute",label:"单位属性",required:true},
		{type:"input",name:"securityPerson",label:"网络安全责任人",maxLength:5,required:true},
		{type:"input",name:"securityPersonID",label:"责任人身份证号",required:true},
		{type:"input",name:"securityPhone",label:"责任人电话",maxLength:12,required:true},
		{type:"input",name:"securityEmail",label:"责任人邮箱",validate:"ValidEmail",required:true},
	]},
];

var ipStructure = {type:"input",label:"互联ip",name:"ip",validate:"ipstr",required:true};
var	vlanStructure = {type:"input",label:"vlan",name:"vlan"/*,validate:"^[2-3][0-9]{3}$",required:true*/};
var buttons = {type:"button",name:"auto_pre",value:"自动预分配"};
	/* 初始化combo */
	function initCombo() {
		var combo;
		for (var cfg_d in config["default"]) {
			myForm.reloadOptions(cfg_d, config["default"][cfg_d]);
			myForm.getCombo(cfg_d) && myForm.getCombo(cfg_d).enableFilteringMode("between");
		}
		for (var cfg_m in config["muticolumn"]) {
			combo = myForm.getCombo(cfg_m);
			if (null == combo) {
				continue;
			}
			combo.clearAll();
			combo.setTemplate({
				input: "#id#-#MC#",
				//header:false,//if you want to hide header
				columns: [
					{ header: "id", width: 60, option: "#id#" },
					{ header: "MC", width: 150, option: "#MC#" }
				]
			});
			combo.addOption(config["muticolumn"][cfg_m]);
			combo.enableFilteringMode("between");
		}
	}
	/* 设置 onChange 事件 */
	function onChangeHandler(name, value, status) {
		if (name == 'neFactory') {
			if (value == 'ONU') {
				myForm.showItem("oltName");
				myForm.setItemValue("ifOnu", true);
				myForm.enableItem("oltName");
				if (!/todo/.test(location.href)) {
					myForm.getCombo("aStation").setComboText("");
					myForm.getCombo("aStation").setComboValue("");
					//myForm.disableItem("aStation");
					myForm.hideItem("aStation");
					/*myForm.setNote("neFactory", {
						text: "ONU的业务无需选择A端基站"
					});*/

				}
			} else {
				myForm.setItemValue("ifOnu", false);
				myForm.disableItem("oltName");
				myForm.hideItem("oltName");
				myForm.enableItem("aStation");
				myForm.showItem("aStation");
				/*myForm.setNote("neFactory", {
					text: null
				});*/
			}
		}
		if (name == 'ifOnu') {
			if (status == false) {
				if (myForm.getItemValue("neFactory") == 'ONU') {
					myForm.setItemValue("ifOnu", true);
					myForm.enableItem("oltName");
					dhtmlx.alert("网元厂家为 ONU 时此项必选");
				}
			} else {
				if (myForm.getItemValue("neFactory") == 'ONU') { } else {
					// 勾选了ifOnu，但网元厂家选的不是ONU。
					myForm.setItemValue("neFactory", 'ONU');
					myForm.enableItem("oltName");
					myForm.getCombo("aStation").setComboText("");
					myForm.getCombo("aStation").setComboValue("");
					myForm.setNote("neFactory", {
						text: "ONU 时无需选择 A 端基站"
					});
					myForm.disableItem("aStation");
					dhtmlx.alert({
						title: "ONU 的业务<span style='color:red;'>无需选择</span> A 端基站",
						text: "已自动修改<span style='color:red;'>网元厂家</span>字段为 ONU<br>并去除<span style='color:red;'> A 端基站</span>字段"
					});
				}
			}
		}
		if (name == 'unitProperty') {
			myForm.setItemValue('unitAttribute', myForm.getCombo('unitProperty').getComboText().substr(2));
		}
		if (['cPerson', 'cPhone', 'cEmail'].indexOf(name) != -1) {
			var _key = 'security' + name.substr(1);
			myForm.setItemValue(_key, myForm.getItemValue(name));
		}
		if (name == 'county') {
			myForm.setItemValue('zipCode', config['zipCode'][myForm.getItemValue(name)]);
		}
	}
	/* 表单字段去空格 */
	function removeSpace() {
		var _tmp = "";
		var _new = "";
		var cal = 0;
		for (var i in myForm.getFormData()) {
			_tem = myForm.getFormData()[i] + "";
			_new = _tem.replace(/ /g, "");
			if (_tem != _new) {
				cal++;
			}
			myForm.setItemValue(i, _new);
		}
		return cal;
	}
	/**
	 * 验证是否符合ip字符串的格式:10.10.10.10/32
	 */
	function ipstr(value) {
		var a = value.split("/");
		var c = a[0].toString().match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
		var b = a.length == 1 ? true : a.length > 2 ? false : parseInt(a[1]) <= 32 && a[1].match(/^\d{0,2}$/);
		return c && !!(c[1] <= 255 && c[2] <= 255 && c[3] <= 255 && c[4] <= 255) && b || false;
	}
	/* 修补新增的额外数据 */
	function fixExtraFields(){
		if (myForm.getItemValue("neFactory") == "ONU") {
			myForm.getCombo("aStation").setComboText("");
			myForm.getCombo("aStation").setComboValue("");
			myForm.setNote("neFactory", {
				text: "ONU 的业务无需选择 A 端基站"
			});
			myForm.disableItem("aStation");
		}
		if(myForm.getItemValue('unitAttribute') == '') {
			myForm.setItemValue('unitAttribute', myForm.getCombo('unitProperty').getComboText().substr(2));
		}
		['cPerson', 'cPhone', 'cEmail'].map(function(name){
			var _key = 'security' + name.substr(1);
			myForm.getItemValue(_key) == '' && myForm.setItemValue(_key, myForm.getItemValue(name));
		});
		if (myForm.getItemValue('zipCode') == '') {
			myForm.setItemValue('zipCode', config['zipCode'][myForm.getItemValue('county')]);
		}
	}
</script>